/*
 * @Author: liuYang codkly@gmail.com
 * @Date: 2022-11-09 09:24:12
 * @LastEditors: liuYang codkly@gmail.com
 * @LastEditTime: 2022-11-09 09:26:56
 * @FilePath: \coding\blog\blog_server\src\controllers\article.js
 * @Description:
 * 暂无备注
 */
const { sequelize, Sequelize } = require("../common/dbs");
var Op = Sequelize.Op;

const articleModel = require("../models/article");
const articleInfo = articleModel(sequelize);

const classifyModel = require("../models/classify");
const classifyInfo = classifyModel(sequelize);

const { getUserId, getImgUrl } = require("../common/utils");

module.exports = ({ $services }) => {
  return {
    /**
     * @description: 热门
     * @return {*}
     */
    hot: async (ctx, next) => {
      try {
        let reqQuery = ctx.request.query;
        let reData = await articleInfo.findAndCountAll({
          where: { status: 1 },
          attributes: ["id", "title", "views", "desc", "created_at"],
          order: [
            //排序 DESC ASC
            ["views", "DESC"],
          ],
          limit: reqQuery.limit ? parseInt(reqQuery.limit) : 6,
          offset: reqQuery.page > 0 ? (parseInt(reqQuery.page) - 1) * 6 : 0,
        });
        ctx.success(reData);
      } catch (err) {
        ctx.fail(0, err);
      }
    },
  };
};
